//
//  YKPublishViewController.m
//  baisi
//
//  Created by 吴高烽 on 16/10/12.
//  Copyright © 2016年 吴高烽. All rights reserved.
//

#import "YKPublishViewController.h"
#import "YKVerticalButton.h"
#import "YKPostViewController.h"
#import "YKNavigationController.h"
#import <POP.h>
@interface YKPublishViewController ()
@property (nonatomic ,weak)UIImageView *slogaView;

@end
static CGFloat const YKAnimationDelay = 0.1 ;
static CGFloat const YKAnimSpringFactor  = 8 ;
@implementation YKPublishViewController
+(instancetype)publishView{
    return [[[NSBundle mainBundle]loadNibNamed:NSStringFromClass(self) owner:nil options:nil] lastObject];
}
-(void)awakeFromNib{
    [super awakeFromNib];
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.userInteractionEnabled = NO ;
    // 添加logo
    UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"app_slogan"]];
    CGFloat imageVEY = 110;
    CGFloat imageVOX = SCWidth / 2 ;
    CGFloat imageVOY = imageVEY - SCHeight ;
    imageView.center = CGPointMake(imageVOX, imageVOY);
    [self.view addSubview:imageView];
    self.slogaView = imageView ;
    // 数据
    NSArray *images = @[@"publish-video", @"publish-picture", @"publish-text", @"publish-audio", @"publish-review", @"publish-offline"];
    NSArray *titles = @[@"发视频", @"发图片", @"发段子", @"发声音", @"审帖", @"离线下载"];
    NSInteger maxCols = 3;
    CGFloat btnW = 72;
    CGFloat btnH = btnW +30;
    CGFloat btnOX = 15 ;
    CGFloat btnY = SCHeight * 0.35 ;
    CGFloat btnM = (SCWidth - 2 * btnOX - maxCols * btnW) / (maxCols - 1) ;
    for (NSInteger i = 0; i < titles.count; i++) {
        YKVerticalButton * btn = [YKVerticalButton buttonWithType:UIButtonTypeCustom];
        [btn addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        btn.tag = i ;
        [self.view addSubview:btn];
        [btn setImage:[UIImage imageNamed:images[i]] forState:UIControlStateNormal];
        [btn setTitle:titles[i] forState:UIControlStateNormal];
        btn.titleLabel.font = [UIFont systemFontOfSize:13];
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        CGFloat btnX = btnOX+(i%maxCols*(btnW+btnM)) ;
        CGFloat btnEY = btnY+(i/maxCols*(btnH+btnOX)) ;
        CGFloat btnOY = btnEY - SCHeight ;
   //     btn.frame = CGRectMake(btnX+(i%maxCols*(btnW+btnM)), btnY+(i/maxCols*(btnH+btnX)), btnW, btnH);
        POPSpringAnimation *anim = [POPSpringAnimation animationWithPropertyNamed:kPOPViewFrame];
        anim.fromValue = [NSValue valueWithCGRect:CGRectMake(btnX, btnOY, btnW, btnH)] ;
        anim.toValue =  [NSValue valueWithCGRect:CGRectMake(btnX, btnEY, btnW, btnH)] ;
        anim.springSpeed = YKAnimSpringFactor;
        anim.springBounciness = YKAnimSpringFactor;
        anim.beginTime = CACurrentMediaTime() + (images.count - i)*YKAnimationDelay ;
        [btn pop_addAnimation:anim forKey:nil];
    }
    POPSpringAnimation *animL = [POPSpringAnimation animationWithPropertyNamed:kPOPViewCenter];

    animL.fromValue = [NSValue valueWithCGPoint:CGPointMake(imageVOX, imageVOY)];
    animL.toValue =[NSValue valueWithCGPoint:CGPointMake(imageVOX, imageVEY)];
    animL.springSpeed = YKAnimSpringFactor ;
    animL.springBounciness = YKAnimSpringFactor ;
    animL.beginTime = CACurrentMediaTime() + images.count *YKAnimationDelay ;
    
    [imageView pop_addAnimation:animL forKey:nil];
    [animL setCompletionBlock:^(POPAnimation *anim, BOOL finish) {
        if (finish) {
            self.view.userInteractionEnabled = YES ;
        }
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)back:(id)sender {
    [self cancelWithCompletionBlock:nil];
  
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self cancelWithCompletionBlock:nil];
}
-(void)buttonClick:(UIButton*)btn{
    [self cancelWithCompletionBlock:^{
        if (btn.tag == 0) {
            YKLog(@"发视频");
        }else if (btn.tag ==1 ){
            YKLog(@"发图片");
        }else if (btn.tag ==2 ){
            YKPostViewController *postVC = [[YKPostViewController alloc]init];
            YKNavigationController *nav = [[YKNavigationController alloc]initWithRootViewController:postVC];
            UIViewController *root = YKKeyWindow.rootViewController ;
            [root presentViewController:nav animated:YES completion:nil];
            YKLog(@"发段子");
        }else if (btn.tag ==3 ){
            YKLog(@"发声音");
        }else if (btn.tag ==4 ){
            YKLog(@"审帖");
        }else if (btn.tag ==5 ){
            YKLog(@"发连接");
        }
    }];

}
-(void)cancelWithCompletionBlock:(void(^)())completionBlock{
    self.view.userInteractionEnabled = NO ;
    NSInteger count  = self.view.subviews.count ;
    NSInteger index = 2 ;
    for (NSInteger i = index; i<count; i++) {
        UIView *view = self.view.subviews[i];
        POPBasicAnimation *anim = [POPBasicAnimation animationWithPropertyNamed:kPOPViewCenter];
        CGFloat centerY = view.centerY + SCHeight ;
        anim.beginTime = CACurrentMediaTime() + (i-index)*YKAnimationDelay ;
        anim.toValue = [NSValue valueWithCGPoint:CGPointMake(view.centerX, centerY)];
        [view pop_addAnimation:anim forKey:nil];
        if (i == count-1 ) {
            [anim setCompletionBlock:^(POPAnimation *anim, BOOL finish) {
                [self dismissViewControllerAnimated:NO completion:nil];
                !completionBlock ? : completionBlock();
            }];
        }
    }
}
/*
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    POPSpringAnimation *anim = [POPSpringAnimation animationWithPropertyNamed:kPOPLayerPositionY];
  //  anim.fromValue = @(1.0);
  //  anim.toValue = @(0.0);
    anim.beginTime = CACurrentMediaTime() + 0.5 ;

    anim.springSpeed = 20;
    anim.springBounciness = 20 ;
    anim.fromValue =@(0);
    anim.toValue = @(self.slogaView.layer.position.y);
  //anim.completionBlock
    [self.slogaView.layer pop_addAnimation:anim forKey:@"slogaView"];

}*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
